package com.tanovo.service.impl;

import com.tanovo.domain.Resources;
import com.tanovo.domain.ResourcesExample;
import com.tanovo.mapper.ResourcesMapper;
import com.tanovo.service.IResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class ResourceImpl implements IResource {
    @Autowired
    private ResourcesMapper resourcesMapper;

    @Override
    public boolean checkNoTokenResourceList(String realPath) {
        ResourcesExample example = new ResourcesExample();
        example.createCriteria().andCheckFlagEqualTo(0)   //0:无权限，1:校验用户，2：检查权限
                .andDelFlagEqualTo(0);
        List<Resources> resourcesList = resourcesMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(resourcesList)) {
            return false;
        }
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        for (Resources re : resourcesList) {
            boolean match = antPathMatcher.match(re.getPath(), realPath);
            if(match){
                return Boolean.TRUE;
            }
        }
        return false;
    }

    @Override
    public boolean checkLoginResourceList(String realPath) {
        ResourcesExample example = new ResourcesExample();
        example.createCriteria().andCheckFlagEqualTo(1)   //0:无权限，1:校验用户，2：检查权限
                .andDelFlagEqualTo(0);
        List<Resources> resourcesList = resourcesMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(resourcesList)) {
            return false;
        }
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        for (Resources re : resourcesList) {
            boolean match = antPathMatcher.match(re.getPath(), realPath);
            if(match){
                return true;
            }
        }
        return false;
    }
}
